package sg.edu.nus.comp.qa.utils;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class StopWordsSet {
	private static Log LOGGER = LogFactory.getLog(StopWordsSet.class);
	private static String STOP_WORD_FILE = "target/classes/stopword_list";
	private static StopWordsSet SINGLETON;

	private static final Set<String> STOP_WORD_SET = new HashSet<String>();

	private static void init() {
		SINGLETON = new StopWordsSet();
		try {
			Scanner sc = new Scanner(new File(STOP_WORD_FILE));
			while (sc.hasNext()) {
				STOP_WORD_SET.add(sc.nextLine());
			}
			sc.close();
		} catch (FileNotFoundException e) {
			LOGGER.error("FileNotFound", e);
		}

	}

	public boolean isStopWord(String word) {
		return STOP_WORD_SET.contains(word.toLowerCase());
	}

	public String removeStopWords(String string) {
		String[] words = string.split("[^a-zA-Z\\d:]");
		StringBuilder builder = new StringBuilder();
		for (String word : words) {
			if (!isStopWord(word))
				builder.append(word).append(" ");
		}
		return builder.toString().trim();
	}

	public static StopWordsSet getStopWordSet() {
		if(SINGLETON == null)
			init();
		return SINGLETON;
	}
}
